package com.singlelist;

import java.util.LinkedList;

public class SingleListDemo {
    public static void main(String[] args) {
        SingleList list = new SingleList();
        //add方法没有问题
        list.add(new UserNode(1,"KAGE"));
        list.add(new UserNode(2,"AKUMA"));
        list.add(new UserNode(3,"CHUN-LI"));
        list.add(new UserNode(4,"KEN"));
        list.show();

        System.out.println("删除-------------------------");
        list.remove(2);
        list.show();

        System.out.println("修改---------------------------");
        list.update(new UserNode(4,"ryu"));
        list.show();

        System.out.println("查询-----------------------");
        System.out.println(list.findById(3));

        System.out.println("链表长度="+list.size());
    }
}

//模拟单链表
class SingleList{
    private int size;
    //定义头节点
    private UserNode head = new UserNode(0,"");
    public void setHead(UserNode head) {
        this.head = head;
    }

    //判断链表是否为空
    public boolean isEmpty(){
        if(head.next == null){
            return true;//此时链表为空
        }
        return false;
    }

    //增
    public void add(UserNode userNode){
        //定义一个空指针，指向head
        UserNode temp = head;
        while (temp.next != null){
            //指针后移
            temp = temp.next;
        }
        temp.next = userNode;
    }
    //删
    public boolean remove(int id){
        //如果为空
        if(isEmpty()){
            System.out.println("链表为空不能删除");
            return false;
        }
        //遍历链表
        //空指针指向头节点的下一个
        UserNode temp = head.next;
        while (temp != null){
            //找到了
            //这里注意一下temp指针的走向
            if(temp.next.id == id){
                temp.next = temp.next.next;
                return true;
            }
            temp = temp.next;
        }
        System.out.println("没有找到对应节点");
        return false;
    }

    //改
    public void update(UserNode userNode){
        if(isEmpty()){
            System.out.println("链表为空");
            return;
        }
        UserNode temp = head.next;
        while (temp != null){
            if(temp.id == userNode.id){
                temp.name = userNode.name;
                return;
            }
            temp = temp.next;
        }
    }
    //查
    public UserNode findById(int id){
        if(isEmpty()){
            System.out.println("链表为空");
            return null;
        }
        UserNode temp = head.next;
        while (temp != null){
            if(temp.id == id){
                return temp;
            }
            temp = temp.next;
        }
        System.out.println("没有找到对应的节点");
        return null;
    }

    //显示链表
    public void show(){
        if(isEmpty()){
            System.out.println("链表为空");
            return;
        }
        //输出节点信息
        UserNode temp = head.next;
        while (temp  != null){
            System.out.println(temp);
            temp = temp.next;
        }
    }

    //得到链表的长度
    public int size(){
        UserNode temp = head.next;
        while (temp != null){
            size++;
            temp = temp.next;
        }
        return size;
    }
}

//定义节点
class UserNode {
    public int id;
    public String name;
    public UserNode next;

    public UserNode(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "MyNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

